﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using DTO;
namespace DAO
{
    public class PhieuDatPhongDAO:Abstract
    {
        public static bool LapPhieuDatPhong(PhieuDatPhongDTO phieuDatPhong)
        {
            String query = "insert into PhieuDatPhong ( TenNguoiDat, ThoiGianDat, MaLoaiPhong,DaNhanPhong) values ('{1}','{2}',{3},{4})"
                .Replace("{1}", phieuDatPhong.TenNguoiDat.ToString())
                .Replace("{2}", phieuDatPhong.ThoiGianDat.ToShortDateString())
                .Replace("{3}", phieuDatPhong.MaLoaiPhong.ToString())
                .Replace("{4}","0");
            return Query(query);
        }

        public static bool SuaPhieuDatPhong(PhieuDatPhongDTO phieuDatPhong)
        {
            String query = "update PhieuDatPhong set MaLoaiPhong = {0},ThoiGianDat = '{1}', TenNguoiDat = '{2}',DaNhanPhong = {3} where MaPhieuDatPhong = {4}"
                .Replace("{0}", phieuDatPhong.MaLoaiPhong.ToString())
                .Replace("{1}",phieuDatPhong.ThoiGianDat.ToShortDateString())
                .Replace("{2}",phieuDatPhong.TenNguoiDat)
                .Replace("{3}",phieuDatPhong.DaNhanPhong == true? "1":"0")
                .Replace("{4}", phieuDatPhong.MaPhieuDatPhong.ToString()) ;

            return Query(query);
        }
        public static bool XoaPhieuDatPhong(int _maPhieuDatPhong)
        {
            string query = "delete from PhieuDatPhong where MaPhieuDatPhong = {1}"
                .Replace("{1}", _maPhieuDatPhong.ToString());
            return Query(query);
        }
        public static List<PhieuDatPhongDTO> LayDSPhieuDatPhong(int maPhieuDatPhong = -1, string tenNguoiDat = null,int daNhanPhong = -1,int _maLoaiPhong = -1)
        {
            OleDbConnection conn = null;
            List<PhieuDatPhongDTO> listPhieuDatPhong = new List<PhieuDatPhongDTO>();
            try
            {
                conn = GetConnection();
                conn.Open();
            }
            catch
            {
                throw new Exception("Connect error!");
            }
            try
            {
                string query = "select MaPhieuDatPhong, TenNguoiDat,MaLoaiPhong, ThoiGianDat ,DaNhanPhong from PhieuDatPhong ";
                if (maPhieuDatPhong != -1)
                    query = query + "where MaPhieuDatPhong = {0}".Replace("{0}", maPhieuDatPhong.ToString());
                else if (tenNguoiDat != null)
                    query = query + "where TenNguoiDat = '{1}'".Replace("{1}", tenNguoiDat);
                else if (daNhanPhong != -1)
                    query = query + "where DaNhanPhong = {2}".Replace("{2}", daNhanPhong == 1? "Yes":"No");
                else if (_maLoaiPhong != -1)
                    query = query + "where MaLoaiPhong = {3}".Replace("{3}", _maLoaiPhong.ToString());
                OleDbDataReader rd = new OleDbCommand(query, conn).ExecuteReader();
                while (rd.Read())
                {
                    PhieuDatPhongDTO phieuDatPhong = new PhieuDatPhongDTO();
                    phieuDatPhong.MaPhieuDatPhong = rd.GetInt32(0);
                    phieuDatPhong.TenNguoiDat = rd.GetString(1);
                    phieuDatPhong.MaLoaiPhong = rd.GetInt32(2);
                    phieuDatPhong.ThoiGianDat = rd.GetDateTime(3);
                    phieuDatPhong.DaNhanPhong = rd.GetBoolean(4);

                   if(_maLoaiPhong == -1)
                   {
                        LoaiPhongDTO p = LoaiPhongDAO.LayDSLoaiPhong(phieuDatPhong.MaLoaiPhong)[0];
                        phieuDatPhong.TenLoaiPhong = p.TenLoaiPhong;
                       
                    }
                   listPhieuDatPhong.Add(phieuDatPhong);
                }

            }
            catch (Exception e)
            {
                listPhieuDatPhong = null;
            }

            if (conn.State == System.Data.ConnectionState.Open)
                conn.Close();
            return listPhieuDatPhong;
        }

    }
}
